<template>
	<view class="newAddressPage content">
		<view class="row b-b">
			<text class="tit">收货人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="请填写收货人姓名"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">联系电话</text>
			<input class="input" type="number" v-model="addressData.mobile" placeholder="请填写联系电话"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">所在地区</text>
			<picker class="addpicker" @change="bindPickerChange" :value="addressData.region" mode="region">
				<view class="picker">
				  {{addressData.region.length > 0 ? addressData.region[0] + ' ' + addressData.region[1] + ' ' + addressData.region[2] : '省、 市、 区' }}
				</view>
			</picker>
			
			<text class="yticon icon-shouhuodizhi"></text>
		</view>
		<view class="row b-b row-add">
			<text class="tit">详细地址</text>
			<textarea class="text-wrapper"  v-model="addressData.address" placeholder="请填写详细地址信息、如街道、小区、楼栋号、单元室等">
				<!-- <input class="input" type="text" v-model="addressData.address" placeholder="请填写详细地址信息、如街道、小区、楼栋号、单元室等"
					placeholder-class="placeholder" /> -->
			</textarea>
		</view>

		<view class="row default-row">
			<text class="tit">设置默认地址</text>
			<switch :checked="addressData.default" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">保存并使用</button>
	</view>
</template>

<script>
	import { getUser } from '@/utils/vmall.js';
	export default {
		data() {
			return {
				region: [],
				addressData: {
					name: '',
					mobile: '',
					region: [],
					address: '',
					area: '',
					default: false
				},
				userInfo: null
			}
		},
		onLoad(option) {
			let title = '新增地址';
			if (option.type === 'edit') {
				title = '编辑收货地址'
				this.setAddressData(option.data)
				
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
			getUser().then(info => {
				this.userInfo = info
			})
		},
		methods: {
			setAddressData(data) {
				let addinfo = JSON.parse(data)
				let address = addinfo.address
				let regions = address.split(' ')
				let si = ''
				let sindex = 0
				let region = []
				let naddress = ''
				if (regions.length <= 3) {
					for (let i=0; i<address.length;i++) {
						si+=address[i]
						if (si.length>2) {
							if (['省','市','区','县','州'].indexOf(address[i]) != -1 && sindex <= 2) {
								region[sindex] = si
								if (sindex == 2) naddress = address.substr(i+1).replace(/(^\s*)|(\s*$)/g, '')
								si = ''
								sindex++
								if (sindex>=3) break
							}
						}
					}
				} else {
					region = regions.length > 3 ? [regions[0], regions[1], regions[2]] : [],
					naddress = regions.slice(3).join(' ')
				}
				
				console.log('regions', address, region)
				addinfo.region = region
				addinfo.address = naddress
				this.addressData = addinfo
			},
			bindPickerChange(e) {
				console.log('picker', e)
				this.addressData.region = e.detail.value
			},
			switchChange(e) {
				this.addressData.default = e.detail.value;
			},
			//提交
			confirm() {
				let data = this.addressData;
				console.log('add data', data)
				if (!data.name) {
					this.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.mobile)) {
					this.msg('请输入正确的手机号码');
					return;
				}
				if (data.region.length == 0) {
					this.msg('请选择所在省市区');
					return;
				}
				if (!data.address) {
					this.msg('请填写详细地址');
					return;
				}
				let address = data.region[0] + ' ' + data.region[1] + ' ' + data.region[2] + ' ' + data.address
				let isDefault = data.default == false ? 0 : 1 // 0-否 1-是
				let formData = {
					name: data.name,
					mobile: data.mobile,
					address,
					isDefault,
					userId: this.userInfo.id
				}
				if (this.manageType=='edit') {
					formData.id = data.id
				}
				let apiurl = this.manageType=='edit' ? this.$api.editAddress : this.$api.addAddress
				this.$request.post(apiurl, formData).then(res => {
					// 加载状态重置
					uni.hideLoading()
					const { success, result } = res
					console.log('add result', res)
					if (success) {
						this.$appFunc.prePage().refreshList(data, this.manageType);
						this.$appFunc.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					}  else {
						this.$appFunc.msg('添加失败')
					}
				})
				
				
				//this.$api.prePage()获取上一页实例，可直接调用上页所有数据和方法，在App.vue定义
				// this.$api.prePage().refreshList(data, this.manageType);
				// this.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
				// setTimeout(() => {
				// 	uni.navigateBack()
				// }, 800)
			},
			msg(title, duration = 1500, mask = false, icon = 'none') {
				//统一提示方便全局修改
				if (Boolean(title) === false) {
					return;
				}
				uni.showToast({
					title,
					duration,
					mask,
					icon
				});
			},
			prePage() {
				let pages = getCurrentPages();
				let prePage = pages[pages.length - 2];
				// #ifdef H5
				return prePage;
				// #endif
				return prePage.$vm;
			}
		}
	}
</script>

<style lang="scss">
	@import "@/style/variable.scss";
	page {
		background: $page-color-base;
		padding-top: 16upx;
	}
	.newAddressPage {
		.row {
			display: flex;
			align-items: center;
			position: relative;
			padding: 0 30upx;
			height: 110upx;
			background: #fff;
		
			.tit {
				flex-shrink: 0;
				width: 120upx;
				font-size: 30upx;
				color: $font-color-dark;
			}
		
			.input {
				flex: 1;
				font-size: 30upx;
				color: $font-color-dark;
				text-align: right;
			}
		
			.icon-shouhuodizhi {
				font-size: 36upx;
				color: $font-color-light;
			}
			.picker{
				text-align: right;
				font-size: 30upx;
			}
		}
		.row-add{
			height:440upx;
			align-items: flex-start;
		}
		.row-add .input-wrapper{
			height:440upx;
			// -webikit-line-clamp:5;
			// text-overflow: ellipsis;
			// display: -webkit-box;
			// -webkit-box-orient: vertical;
			// overflow: hidden;
			.input{
				height:440upx;
				flex:auto;
				-webikit-line-clamp:5;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				overflow: hidden;
			}
		}
		
		.default-row {
			margin-top: 16upx;
		
			.tit {
				flex: 1;
			}
		
			switch {
				transform: translateX(16upx) scale(.9);
			}
		}
		
		.add-btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 690upx;
			height: 80upx;
			margin: 60upx auto;
			font-size: $font-lg;
			color: #fff;
			border-radius: 0;
			background-color: $base-color;
		}
		.add-btn::after{
			border-radius: 0;
		}
		.addpicker {
			width: 550rpx;
		}
		.text-wrapper {
			padding-left: 120rpx;
		}
	}
	
</style>
